package com.sincebest.fivestar.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.StringUtil;
import com.sincebest.fivestar.common.CommandEnum;
import com.sincebest.fivestar.common.RawErrorCodeException;
import com.sincebest.fivestar.common.ResultResponse;
import com.sincebest.fivestar.common.RobotCommand;
import com.sincebest.fivestar.model.RobotWxGroupRela;
import com.sincebest.fivestar.model.WechatGroupMember;
import com.sincebest.fivestar.service.RobotService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * Created by peter on 17/3/28
 */
@Api(value = "robot", description = "机器人接口")
@RestController
@RequestMapping(value = "/robot", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class RobotController extends BaseController {

	@Autowired
	private RobotService robotService;

	/**
	 * 微信号绑定机器人，如果已经绑定则直接返回绑定的机器人号
	 * 
	 * @return
	 */
	@RequestMapping(value = "/binding-robot", method = RequestMethod.GET)
	@ApiOperation(value = "微信号绑定机器人")
	public ResultResponse<RobotCommand> bindingRobot(
			@ApiParam(value = "微信号的imId") @RequestParam(value = "imId", required = true) String imId,
			@ApiParam(value = "机器人Id") @RequestParam(value = "robotId", required = false) Integer robotId) {
		Integer bingdingRobotId = robotService.bindingRobot(imId, robotId);
		RobotCommand command = new RobotCommand();
		command.setCommand(CommandEnum.EMPTY);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("robotId", bingdingRobotId);
		command.setParam(map);
		return ResultResponse.ok(command);
	}

	@RequestMapping(value = "/verify-friend-wx", method = RequestMethod.GET)
	@ApiOperation(value = "验证好友是不是代理")
	public ResultResponse<RobotCommand> verifyFriendWX(
			@ApiParam(value = "要验证的人的微信Id") @RequestParam(value = "wxId", required = true) String wxId) {
		Boolean isHouseOwner = robotService.verifyIsHouseOwner(null, wxId);
		RobotCommand command = new RobotCommand();
		if (isHouseOwner) {
			command.setCommand(CommandEnum.AGREE_FRIEND_AND_SEND_MSG);
			command.setTargetId(wxId);//
			command.setContent("你好，我是您的麻将馆助手小胡。您需要把我拉入您的麻将馆对应的微信群，并给我发送绑定指令来绑定您的麻将馆和微信群。我即可为您的麻将馆提供服务。\n"
					+"操作步骤：\n"
					+"一、拉我入群\n"
					+"二、我入群之后在群里发送：@小胡 绑定 xxxxx (xxxx就是您的麻将馆id)\n"
					+"注意事项：您必须是群主的身份才能通过以上操作指令完成绑定。否则命令无效 ");
		} else {
			command.setCommand(CommandEnum.EMPTY);
		}
		return ResultResponse.ok(command);
	}

	@RequestMapping(value = "/service-for-wxgroup", method = RequestMethod.POST)
	@ApiOperation(value = "机器人绑定微信群")
	public ResultResponse<RobotCommand> serviceForWxGroup(
			@ApiParam(value = "机器人Id") @RequestParam(value = "robotId", required = true) Integer robotId,
			@ApiParam(value = "微信群Id") @RequestParam(value = "groupId", required = true) String groupId,
			@ApiParam(value = "群主的微信Id") @RequestParam(value = "ownerWxId", required = true) String ownerWxId) {
		RobotCommand command = new RobotCommand();
		command.setCommand(CommandEnum.EMPTY);
		Integer result = robotService.bindingWxGroup(robotId, groupId, ownerWxId);
		return ResultResponse.ok(command);
	}

	@RequestMapping(value = "/binding-house", method = RequestMethod.POST)
	@ApiOperation(value = "微信群绑定麻将馆")
	public ResultResponse<RobotCommand> bindingHouse(
			@ApiParam(value = "微信群Id") @RequestParam(value = "groupId", required = true) String groupId,
			@ApiParam(value = "麻将馆id") @RequestParam(value = "houseId", required = true) String houseId) {
		RobotCommand command = new RobotCommand();
		command.setCommand(CommandEnum.SEND_MSG);

		try{
			Integer result = robotService.bindingHouse(groupId, houseId);
			command.setContent("绑定麻将馆成功");
		}catch(RawErrorCodeException e){
			command.setContent("绑定麻将馆失败"+e.getErrorCode().getMessage()+", 错误码："+e.getErrorCode().getCode());
		}catch(Exception e){
			command.setContent("绑定麻将馆失败,"+e.getMessage());
		}
		RobotWxGroupRela rela = robotService.queryWxGroupRela(groupId);
		command.setTargetId(rela.getGroupOwnerId());
		return ResultResponse.ok(command);
	}

	@RequestMapping(value = "/add-group-members", method = RequestMethod.POST)
	@ApiOperation(value = "微信群添加成员")
	public ResultResponse<RobotCommand> addGroupMembers(
			@ApiParam(value = "群成员") @RequestBody List<WechatGroupMember> members) {

		RobotCommand command = new RobotCommand();
		if (!CollectionUtils.isEmpty(members)) {
			String inviteUrl = robotService.addWxGroupMembers(members);
			WechatGroupMember firstMember = members.get(0);

			if (StringUtil.isNotEmpty(inviteUrl)) {
				command.setCommand(CommandEnum.SEND_MSG);
				command.setTargetId(firstMember.getWeChatGroupId());
				command.setContent("邀请进入麻将馆链接：" + inviteUrl);
			} else {
				command.setCommand(CommandEnum.EMPTY);
			}
		} else {
			command.setCommand(CommandEnum.EMPTY);
		}
		return ResultResponse.ok(command);
	}

	@RequestMapping(value = "/kick-group-members", method = RequestMethod.POST)
	@ApiOperation(value = "微信群踢出成员")
	public ResultResponse<RobotCommand> kickGroupMembers(
			@ApiParam(value = "群成员") @RequestBody List<WechatGroupMember> members) {
		RobotCommand command = new RobotCommand();
		if (!CollectionUtils.isEmpty(members)) {
			String kickUrl = robotService.deleteWxGroupMembers(members);
			WechatGroupMember firstMember = members.get(0);
			RobotWxGroupRela rela = robotService.queryWxGroupRela(firstMember.getWeChatGroupId());

			if (StringUtil.isNotEmpty(kickUrl)) {
				command.setCommand(CommandEnum.SEND_MSG);
				command.setTargetId(rela.getGroupOwnerId());// 群主的id
				command.setContent("踢出麻将馆的链接：" + kickUrl);
			} else {
				command.setCommand(CommandEnum.EMPTY);
			}
		} else {
			command.setCommand(CommandEnum.EMPTY);
		}
		return ResultResponse.ok(command);
	}

	@RequestMapping(value = "/update-member-info", method = RequestMethod.POST)
	@ApiOperation(value = "更改群成员信息")
	public ResultResponse<RobotCommand> updateGroupMemberInfo(
			@ApiParam(value = "群成员") @RequestBody WechatGroupMember member) {
		robotService.updateWxGroupMemberInfo(member);

		RobotCommand command = new RobotCommand();
		command.setCommand(CommandEnum.EMPTY);
		return ResultResponse.ok(command);
	}

	@RequestMapping(value = "/update-member-wxId", method = RequestMethod.POST)
	@ApiOperation(value = "更改群成员的微信号")
	public ResultResponse<RobotCommand> updateGroupMemberInfo(
			@ApiParam(value = "修改前的微信号") @RequestParam String oldMemberId,
			@ApiParam(value = "修改后微信号") @RequestParam String newMemberId) {
		robotService.updateWxGroupMemberWxId(oldMemberId, newMemberId);

		RobotCommand command = new RobotCommand();
		command.setCommand(CommandEnum.EMPTY);
		return ResultResponse.ok(command);
	}

}
